/**
 * 
 */
package com.alisoft.couchdbclient

import java.util.*
import org.junit.Assert
import org.junit.*

/**
 * @author dmks
 *
 */
public class CouchDBClientTest{
	
	private CouchDBClient client = new CouchDBClient("10.2.225.129", 5984);
	
	@Before
	void init() {
		if (client.getDatabaseInfo("test") != null) {
			client.deleteDatabase("test");
		}
		client.createDatabase("test");
	}
	
	@After
	void clear() {
		client.deleteDatabase("test");
	}
	
	/* (non-Javadoc)
	 * @see com.alisoft.couchdbclient.CouchDBClient#listDatabases()
	 */
	@Test
	void testListDatabases(){
		println "Result : ${client.listDatabases()}";
	}
	
	@Test
	void testCreateDeleteDatabase() {
		def dbs = client.listDatabases();
		Date now = new Date();
		
		
		100.times { i ->
			client.createDatabase("test${i}");
		}
		Assert.assertEquals(dbs.size() + 100, client.listDatabases().size());
		
		println "Created 100 databases take ${new Date().getTime() - now.getTime()}ms";
		now  = new Date();		
		
		100.times { i ->
			client.deleteDatabase("test${i}");
		}
		
		println "Deleted 100 databases take ${new Date().getTime() - now.getTime()}ms";
		
		try {
			client.createDatabase("test");
			Assert.fail("should throw exception");
		} catch (CouchDBConnectionException e) {
			//
		}
	}
	
	@Test
	void testGetDocumentNonExisting() {		
		def doc = client.getDocument("test", "not_exist");
		Assert.assertNull(doc);		
	}
	
	@Test
	void testDocumentFunctions() {
		String content = """{
		  "Subject":"哈哈",
		  "Author":"Rusty",
		  "PostedDate":"2006-08-15T17:30:12-04:00",
		  "Tags":["plankton", "baseball", "decisions"],
		  "Body":"I decided today that I don't like baseball. I like plankton."
		}""";
		
		def response = client.createDocument("test", content);
		println "response for testCreateDocument - ${response}"
		
		def doc = client.getDocument("test", response.id);
		Assert.assertEquals("哈哈", doc.Subject);
		
		response = client.createDocument("test", "test-id", content);
		doc = client.getDocument("test", "test-id");
		Assert.assertEquals("哈哈", doc.Subject);
		
		println "All revs before deleted - " + client.getDocument("test", "test-id", true, true);
		
		def delRes = client.deleteDocument("test", "test-id", response.rev);
		println "delete test-id result - ${delRes}";
		
		println "All revs - " + client.getDocument("test", "test-id", true, true);
		println "Delete rev for test-id - " + client.getDocument("test", "test-id", response.rev);
		
		
		def obj = [
			"subject" : "hahaha",
			"content" : "bla.. bla...",
			"details" : [["name": "my name", "value": "value 1"],
			             ["name": "my name1", "value": "value 2"]]
		];
		
		response = client.createDocument("test", "test-id", obj);
		
		println "All revs after recreated - " + client.getDocument("test", "test-id", true, true); 
		
		println "-------------------------${response.content} - ${response}";
		
		
		
		def forUpdate = client.getDocument("test", "test-id");
		forUpdate["added"] = '增加';
		println "中文";
		println client.updateDocument("test", "test-id", forUpdate);
		println "after update: ${client.getDocument('test', 'test-id')}"
		
		println "All revs after updated - " + client.getDocument("test", "test-id", true, true);
		
		try {
			client.updateDocument("test", "test-id", forUpdate);
			Assert.fail("should return conflict exception");
		} catch (CouchDBConnectionException e) {
			println e.getMessage();
		}
	
	}
	
}
